const { USER_LIST, USER_LOGIN, USER_REFISTER, USER_INFO } = require("./routerConst");
const { SuccessModel, ErrorModel } = require("../model/resultModel");
const exc = require("../db/mysql");
const userRouterHandler = async (req, res) => {
  if (req.path == USER_LIST) {
    // let resdata;
    let sql = "select * from user";
    try {
      let resdata = await exc(sql)
      return new SuccessModel("获取列表成功", resdata);
    } catch (error) {
      console.error(error);
      return new ErrorModel("获取列表失败", null);
    }
  } else if (req.path === USER_LOGIN) {
    // 获取用户信息
    return new SuccessModel("登录成功", {
      name: "zs",
      age: 33,
    });
  } else if (req.path === USER_REFISTER) {
    let iferror = true;
    // 处理用户注册
    let sql = `insert into user (username,password) values("${req.reqdata.name}","${req.reqdata.password}")`;
    try {
      let resdata = await exc(sql)
      console.log('resdata',resdata);
      
      return new SuccessModel("注册成功", req.reqdata);
    } catch (error) {
      console.error(error);
      return new ErrorModel("注册失败", req.reqdata);
    }
  } else if (req.path === USER_INFO) {
    // 获取用户信息
    return new SuccessModel("用户信息", {
      name: "zs",
      age: 33,
    });
  }
};
module.exports = userRouterHandler;